import { VNode } from 'vue';
import { Dictionary } from '@/typings';
import { Tools } from '@/utils/tools';

export default {
    inserted(el: HTMLElement, binding: Dictionary, vnode: VNode) {
        console.log('v-permission vnode', vnode);
        const { value: permissionObj, arg } = binding;
        const newKey = Tools.toLowerCase(arg);
        if (permissionObj) {
            if (permissionObj.field.includes(newKey)) {
                // 验证是否有编辑权限
                if (!permissionObj.edit.includes(newKey)) {
                    const childrenNodes = el.children[0];
                    const grandsonNodes = childrenNodes.children[0];
                    grandsonNodes.setAttribute('readOnly', 'true');
                }
                // 如果没有浏览权限则隐藏掉
                if (!permissionObj.browse.includes(newKey)) {
                    el.parentNode && el.parentNode.removeChild(el);
                }
            }
        } else {
            throw new Error(`请设置操作权限标签值`);
        }
    }
};
